import {
  addIdentity,
  addUser,
  getUser,
  identity, //  身份数据  /user/identity
  apiAuthority,// api接口权限 /user/api_authority
  identityApiRelation,//身份和api权限关系  /user/identity_api_authority_relation
  getViewAuthoritys,///user/view_authority  试图接口权限
  identityViewAuthorityRelation,//身份和视图关系
} from '@/service';
import {
  IUserName,
  Identity,
  Apiauthority,
  identityApis,
  getViewAuthorityss,
  identityViewAuthorityRelations
} from '@/utils/interface';
import { Effect, Reducer } from 'umi';

export interface IndexModelState {
  userList: IUserName[]
  identitys: Identity[]
  apiauthoritys: Apiauthority[]
  identityApis: identityApis[]
  getViewAuthorityss: getViewAuthorityss[]
  identityViewAuthorityRelationsa:identityViewAuthorityRelations[]
}


export interface IndexModelType {
  namespace: 'people';
  state: IndexModelState;
  effects: {
    setAddUser: Effect;
    getUser: Effect;
    setAddiIdentity: Effect;
    Identity: Effect;
    apiAuthority: Effect;
    identityApi: Effect;
    getViewAutHoritys: Effect
    identityViewAuthorityRelationsa:Effect
  };
  reducers: {
    save: Reducer<IndexModelState>;
  };
}
const PeopleModel: IndexModelType = {
  namespace: 'people',
  state: {
    userList: [],
    identitys: [],
    apiauthoritys: [],
    identityApis: [],
    getViewAuthorityss: [],
    identityViewAuthorityRelationsa:[]
  },
  // 异步操作，理解为vuex中的action, async/await理解为generator函数的语法糖
  effects: {
    *setAddUser({ payload }, { call, put }) {
      let result = yield call(addUser, payload)
      return result
    },
    *getUser({ }, { call, put }) {
      let result = yield call(getUser)
      yield put({
        type: 'save',
        payload: { userList: result.data }
      })
    },
    *setAddiIdentity({ payload }, { call, put }) {
      let result = yield call(addIdentity, payload)
      return result
    },

    //  身份数据  /user/identity
    *Identity({ payload }, { call, put }) {
      let result = yield call(identity, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { identitys: result.data }
        })
      }
    },
    // api接口权限 /user/api_authority
    *apiAuthority({ payload }, { call, put }) {
      let result = yield call(apiAuthority, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { apiauthoritys: result.data }
        })
      }
    },
    //身份和api权限关系  /user/identity_api_authority_relation
    *identityApi({ payload }, { call, put }) {
      let result = yield call(identityApiRelation, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { identityApis: result.data }
        })
      }
    },
    //user/view_authority  试图接口权限
    *getViewAutHoritys({ payload }, { call, put }) {
      let result = yield call(getViewAuthoritys, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { getViewAutHorityss: result.data }
        })
      }
    },

    //identityViewAuthorityRelations
    *identityViewAuthorityRelationsa({ payload }, { call, put }) {
      let result = yield call(identityViewAuthorityRelation, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { identityViewAuthorityRelationsa: result.data }
        })
      }
    }


  },
  // 同步操作，理解为vuex中的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    }
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
};
export default PeopleModel

